﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using FrameWork.DataBaseProvider;
using System.Data.Common;
using Microsoft.Office.Interop.Word;
using Framework.Log;
using RFSerchComponent;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Threading;

namespace IndexService
{
    partial class FileTimeIndexService : ServiceBase
    {
        private Framework.Log.ILog log = Framework.Log.LogFactory.CreateInstance();

        public FileTimeIndexService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档任务启动！"));

                log.WirteLog(string.Concat("\r\n", DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档开始...."));

                //TODO:归档操作-1.锁定实时库 2.归档
                log.WirteLog(string.Concat("\r\n", DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档结束...."));

                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量索引开始【构建xml数据开始】......"));

                //准备创建索引的数据
                int dataCount = IndexCore.PreapareData(EnumServices.FILE_TIME);


                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量索引开始【构建xml数据结束】,共构建数据数：", dataCount, "条"));

                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量【创建索引开始】......"));

                //创建索引，创建完成一个，update一条记录的状态
                string updateIndexSql = "";
                int indexCount = 0;
                if (dataCount <= 0)
                {
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量【创建索引结束（无新增数据）】"));
                    return;
                }

                updateIndexSql = IndexCore.CreateIndex(ref indexCount, EnumServices.FILE_TIME);
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量【创建索引结束】,共更新记录数：", indexCount, "条"));

                if (updateIndexSql != "")
                {
                    bool result = false;
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量更新isXML状态为4【更新数据库开始】......"));
                    result = new DbHelper(Const.FILE_TIME_DBCONNECTION).ExeMoreDB(updateIndexSql);
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库增量更新isXML状态为4【更新数据库结束】更新", result ? "成功" : "失败"));
                }

            }
            catch (Exception ex)
            {
                log.WirteLog(ex.Message);
            }
            finally
            {
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档操作结束....\r\n"));
            }
        }



        protected override void OnStop()
        {
            try
            {
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档库增量停止！"));

            }
            catch (Exception ex)
            {
                log.WirteLog(ex.Message);
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档库增量被终止！"));
            }
        }


    }
}
